package com.yzt.dynamicmodule.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.yzt.dynamicmodule.databinding.ActivityGameScriptDetailBinding;
import com.yzt.plugin_imageloader.ImageLoadUtil;
import com.yzt.zhuju.activity.BaseViewBindingActivity;
import com.yzt.zhuju.arouter.RouterMine;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.network.entity.dynamic.GameScriptDescBean;
import com.yzt.zhuju.network.entity.dynamic.GameScriptDescMissionsBean;
import com.yzt.zhuju.utils.BarUtils;
import com.yzt.zhuju.utils.recyclerutils.GridItemDecoration;
import com.yzt.zhuju.widget.DisableRecyclerView;
import com.yzt.dynamicmodule.R;
import com.yzt.dynamicmodule.adapter.GameScriptDescAdapter;
import com.yzt.zhuju.widget.widget.ImageLoadManger;

/**
 * 作者：bobwang
 * 创建时间：2023-10-12 00:30:18
 * 描述：剧本详情
 */
@SuppressLint("SetTextI18n")
public class GameScriptDetailActivity extends BaseViewBindingActivity<ActivityGameScriptDetailBinding> {

    private int extraId;

    private boolean choosed;

    private boolean isHost;

    private GameScriptDescAdapter adapter;

    @Override
    public boolean hideTitleBar() {
        return true;
    }

    @NonNull
    @Override
    public ActivityGameScriptDetailBinding generateViewBinding() {
        return ActivityGameScriptDetailBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BarUtils.setStatusBarLightMode(this, true);
        BarUtils.setStatusBarAlpha(this, 0);
        isHost = getIntent().getBooleanExtra("extra_host", false);
        extraId = getIntent().getIntExtra("extra_id", 0);
        choosed = getIntent().getBooleanExtra("extra_status", false);
        if (choosed) {
            viewBinding.tvSelect.setVisibility(View.GONE);
            viewBinding.tvUsed.setVisibility(View.VISIBLE);
        } else {
            if (isHost) {
                viewBinding.tvSelect.setVisibility(View.VISIBLE);
            } else {
                viewBinding.tvSelect.setVisibility(View.GONE);
            }
            viewBinding.tvUsed.setVisibility(View.GONE);
        }
        initRecycle();
    }

    private void initRecycle() {
        DisableRecyclerView mReportItemRv = findViewById(R.id.recycler_view);
        mReportItemRv.setLayoutManager(new GridLayoutManager(this, 3));
        mReportItemRv.addItemDecoration(new GridItemDecoration.Builder()
                .divider(10)
                .build());

        adapter = new GameScriptDescAdapter();
        mReportItemRv.setAdapter(adapter);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter1, @NonNull View view, int position) {
                GameScriptDescMissionsBean bean = adapter.getItem(position);
                RouterMine.VideoPlayActivity("2", bean.getVideoUrl());
            }
        });

        initRequest();
        viewBinding.imgBack.setOnClickListener(v -> finish());

        viewBinding.tvSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("extra_id", extraId);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }

    /**
     * 网络请求初始化
     */
    private void initRequest() {
        BizLayer.getInstance().getDynamicModule().scriptDetail(extraId, new AresSubscriber<GameScriptDescBean>() {
            @Override
            protected void onSuccess(GameScriptDescBean bean) {
                ImageLoadManger.loadIv(GameScriptDetailActivity.this, bean.getCover(),
                        viewBinding.imgCover, R.mipmap.default_photo);
                ImageLoadUtil.loadBlur(GameScriptDetailActivity.this, bean.getCover(),
                        viewBinding.imgBlurCover);
                viewBinding.tvName.setText(bean.getTitle());
                viewBinding.tvPeople.setText(bean.getSuggestPeopleNum() + "人");
                viewBinding.tvTime.setText(bean.getSuggestDuration() + "小时");
                viewBinding.tvDesc.setText("剧本简介\n" + bean.getDsc());
                adapter.setList(bean.getMissions());
            }
        });
    }
}
